<?php

function village_plant_list_page()
{
    @$output = drupal_render(drupal_get_form('village_plant_list_form'));
    return $output;
}

function village_plant_list_form($form, &$form_state)
{
    if (!empty($_GET['addr_id'])) {
        $form_state['addr_id'] = $_GET['addr_id'];
        $form['#addr_id'] = $form_state['addr_id'];
    }
    if (empty($form_state['addr_id'])) {
        $form['address_fuzzy'] = _insert_address_autocomplete($form, $form_state);
        $form['address'] = _insert_address_selection_form($form, $form_state, 'address', 5, FALSE, _get_address_id($form_state), '', '', TRUE);
    }
    $form['plant'] = _insert_plant_selection_form($form, $form_state);
    $fields = array(
        '' => '请选择',
        'area' => '种植面积',
        'acre_yield' => '亩产量',
        'price' => '价格',
    );
    numerical_range_search($form, $fields);
    $table['#markup'] = data_list_view_page($form, $form_state);
    ajax_submit_form($form, $form_state, $table, '');
    return $form;
}

function village_plant_list_form_validate($form, &$form_state)
{
}

function data_list_view_page($form = '', &$form_state = '')
{
    global $user;
    $output = '';
    //表头
    $header = array(
        array('data' => '选择', 'class' => ['essential no']),
        isset($form_state['addr_id']) && !empty($form_state['addr_id']) ? "" :
            array('data' => '地址', 'class' => ['essential province city county']),
        array('data' => '农作物', 'class' => ['essential store-name']),
        array('data' => '种植面积', 'class' => ['essential more']),
        array('data' => '亩产量', 'class' => ['essential more']),
        array('data' => '价格', 'class' => ['essential more']),
        array('data' => '提交者', 'class' => ['essential more']),
        array('data' => '创建日期', 'class' => ['essential more']),
        array('data' => '操作', 'class' => ['essential more']),
    );
    $query = db_select('addr', 'A')->extend('PagerDefault')->extend('TableSort');
    $query->join('village_plant', 'V', 'A.addr_id=V.addr_id');

    if (isset($form_state['addr_id']) && !empty($form_state['addr_id'])) {
        $query->condition('V.addr_id', $form_state['addr_id']);
    }

    //地址级别查询
    _address_search_conditon($query,$form_state);
    if (!empty($form_state['values']['plant'])) {//村落级
        $query->condition('V.plant_id', $form_state['values']['plant'], '=');
    }

    if (!empty($form_state['distrubutor_id'])) {
        $query->condition('I.distrubutor_id', $form_state['distrubutor_id']);
    }
    if (!empty($form_state['values']['field'])) {
        if (!empty($form_state['values']['min_number']) && !empty($form_state['values']['max_number'])) {
            $db_and = db_and();
            $db_and->condition('V.' . $form_state['values']['field'], $form_state['values']['min_number'], '>=');
            $db_and->condition('V.' . $form_state['values']['field'], $form_state['values']['max_number'], '<=');
            $query->condition($db_and);
        } elseif (!empty($form_state['values']['min_number'])) {
            $query->condition('V.' . $form_state['values']['field'], $form_state['values']['min_number'], '>=');
        } elseif (!empty($form_state['values']['max_number'])) {
            $query->condition('V.' . $form_state['values']['field'], $form_state['values']['max_number'], '<=');
        }
    }
    if(!user_access('view all village_plant') && user_access('view own village_plant')){
        $query->condition('V.author_uid',$user->uid,'=');
    }
    $query->fields('V');
    $query->leftJoin('addr', 'A1', 'A.province_id=A1.addr_id');
    $query->leftJoin('addr', 'A2', 'A.city_id=A2.addr_id');
    $query->leftJoin('addr', 'A3', 'A.county_id=A3.addr_id');
    $query->leftJoin('addr', 'A4', 'A.town_id=A4.addr_id');
    $query->leftJoin('employee', 'E', 'E.employee_id=V.author_uid');
    $query->leftJoin('plant', 'P', 'P.plant_id=V.plant_id');
    //$query->leftJoin('address', 'town', 'addr.town_id=town.id');
    $query->addField('A', 'name', 'village');
    $query->addField('A1', 'name', 'province');
    $query->addField('A2', 'name', 'city');
    $query->addField('A3', 'name', 'county');
    $query->addField('A4', 'name', 'town');
    $query->addField('E', 'employee_name');
    $query->addField('P', 'plant_name');
    //$query->addField('town', 'name', 'town');
    if(empty($form_state['values']) || empty($form_state['values']['order'])) {
        $query->orderBy('V.timestamp', 'DESC');
    }
    $query->limit(variable_get("list_page_item_number", 5));

    $village_plantInfo = $query->orderByHeader($header)->execute();
    $rows = array();
    //表内容
    $n = 1;
    foreach ($village_plantInfo as $row) {
        $city = $row->city ? ' ' . $row->city : '';
        $county = $row->county ? ' ' . $row->county : '';
        $town = $row->town ? ' ' . $row->town : '';
        $village = $row->village ? ' ' . $row->village : '';
        if (nonghua_user_access('village_plant', 'delete', $row->village_plant_id, $row->author_uid, 0)) {
            $disabled = ' class="listViewTableCheck" ';
        } else{
            $disabled = ' disabled="true" ';
        }
        $rows[] = array(
            array('data' => "<input type='checkbox'".$disabled." value ='" . $row->village_plant_id . "' name='" . $row->village_plant_id . "'/>" . $n++, 'class' => 'integrant essential no'),
            isset($form_state['addr_id']) && !empty($form_state['addr_id']) ? "" :
                array('data' => '<a href="'.base_path().'village/edit/' . $row->addr_id . '?addr_id='.$row->addr_id.'">'.$row->province . $city . $county . $town . $village.'</a>', 'class' => ['integrant haveTobr essential province city county']),
//            array('data' =>'<a href="'.base_path().'village/plant/edit/' . $row->village_plant_id . '">'. $row->province . $city . $county . $town . $village_plant."</a>", 'class' => ['integrant haveTobr essential province city county']),
            array('data' => $row->plant_name, 'class' => ['integrant goRight essential phone']),
            array('data' => $row->area, 'class' => ['integrant goRight essential phone']),
            array('data' => $row->acre_yield, 'class' => ['integrant goRight essential phone']),
            array('data' => $row->price, 'class' => ['integrant goRight essential phone']),
            array('data' => $row->employee_name, 'class' => ['integrant goRight essential phone']),
            array('data' => date("Y-m-d H:i", $row->timestamp), 'class' => ['integrant goRight essential phone']),
            isset($form_state['addr_id']) && !empty($form_state['addr_id']) ?
                array('data' => '<a href="' . base_path() . 'village/plant/edit/' . $row->village_plant_id . '"
             data-toggle="modal" target="_blank"><img width="20px" height="20px" src="' . base_path() . 'sites/all/themes/tweme/images/images/cz.png"/></a>', 'class' => 'essential more') :
                array('data' => '<a href="' . base_path() . 'village/plant/edit/' . $row->village_plant_id . '"  data-toggle="modal"><img width="20px" height="20px" src="' . base_path() . 'sites/all/themes/tweme/images/images/cz.png"/></a>', 'class' => 'essential more'),
        );
    }

    $output .= theme('table', array('header' => $header, 'rows' => $rows, 'empty' => t('没有记录'), 'attributes' => array('class' => array('table table-bordered table-striped table-hover enhanced')))); //表格
    /*	 * *按钮** */
    if (sizeof($rows) > 0 && (user_access('delete own village_plant') ||  user_access('delete all village_plant'))) {
        $output .= '<a href="javascript:void(0)" id="listViewTableSelectAll" data-toggle="modal" class="btn btn-mini">全选</a>';
        $output .= '<a href="javascript:void(0)" id="listViewTableDeleteSelected" data-toggle="modal" class="btn btn-mini">删除所选</a>';
    }
    $output .= theme('pager');
    return $output;
}

function village_plant_delete()
{
    ajax_table_delete('village_plant', 'village_plant_id');
}